﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.PM
{
    public class PMService
    {
        /// <summary>
        /// 创建工作项
        /// </summary>
        /// <param name="parentWorkItem">上一级的工作项</param>
        /// <param name="workItem"></param>
        /// <returns></returns>
        public bool CreateWorkItem(WorkItem parentWorkItem, WorkItem workItem)
        {
            // 保存工作项目本身

            // 创建维度

            return true;
        }

        public Project LoadProject(Guid projectId)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\{1}.dat", AppDomain.CurrentDomain.BaseDirectory, projectId);
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }

            Project project = Utility.SerializeUtility.DeserializeFromBinaryFile(filePath) as Project;
            return project;
        }

        /// <summary>
        /// 获取完整的项目信息，
        /// 工作项类别
        /// 工作项
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public Project LoadFullProject(Guid projectId)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\{1}.dat", AppDomain.CurrentDomain.BaseDirectory, projectId);
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }

            Project project = Utility.SerializeUtility.DeserializeFromBinaryFile(filePath) as Project;
            
            System.IO.DirectoryInfo workItemCategoriesDir = new System.IO.DirectoryInfo(string.Format("{0}\\CacheData\\{1}\\WorkItemCategories\\",
                AppDomain.CurrentDomain.BaseDirectory,
                projectId));
            System.IO.FileInfo[] workItemCategoryFiles = workItemCategoriesDir.GetFiles("*.dat");
            for (int index = 0; index < workItemCategoryFiles.Length; index++)
            {
                project.WorkItemCategoryList.Add(Utility.SerializeUtility.DeserializeFromBinaryFile(workItemCategoryFiles[index].FullName) as WorkItemCategory);
            }

            System.IO.DirectoryInfo workItemsDir = new System.IO.DirectoryInfo(string.Format("{0}\\CacheData\\{1}\\WorkItems\\",
                AppDomain.CurrentDomain.BaseDirectory,
                projectId));
            System.IO.FileInfo[] workItemFiles = workItemsDir.GetFiles("*.dat");
            for (int index = 0; index < workItemFiles.Length; index++)
            {
                project.WorkItemList.Add(Utility.SerializeUtility.DeserializeFromBinaryFile(workItemFiles[index].FullName) as WorkItem);
            }

            return project;
        }

        public void SaveProject(Project project)
        {
            // 找到需要保存的内容，进行保存
            for (int index = 0; index < project.WorkItemCategoryList.Count; index++)
            {
                WorkItemCategory item = project.WorkItemCategoryList[index];
                if (item.IsNeedSave)
                {
                    item.IsNeedSave = false;
                    SaveWorkItemCategory(project.Id, item);
                }
            }
            for (int index = 0; index < project.WorkItemList.Count; index++)
            {
                WorkItem item = project.WorkItemList[index];
                if (item.IsNeedSave)
                {
                    item.IsNeedSave = false;
                    SaveWorkItem(project.Id, item);
                }
            }
            for (int index = 0; index < project.WorkSheetList.Count; index++)
            {
                WorkSheet workSheet = project.WorkSheetList[index];
                if (workSheet.IsNeesSave)
                {
                    workSheet.IsNeesSave = false;
                    CreateWorkSheet(project.Id, workSheet);
                }
            }

            string filePath = string.Format("{0}\\CacheData\\{1}\\{1}.dat", AppDomain.CurrentDomain.BaseDirectory, project.Id);

            project.IsNeedSave = false;
            Utility.SerializeUtility.SerializeToBinaryFile(project, filePath);
        }

        /// <summary>
        /// 读取工作表对象
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="workSheetId"></param>
        /// <returns></returns>
        public WorkSheet LoadWorkSheet(Guid projectId, Guid workSheetId)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\WorkSheets\\{2}.dat", AppDomain.CurrentDomain.BaseDirectory, projectId, workSheetId);
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }

            return Utility.SerializeUtility.DeserializeFromBinaryFile(filePath) as WorkSheet;
        }

        /// <summary>
        /// 创建工作表，当本地不存在时才创建，以后是更新
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="workSheet"></param>
        public void CreateWorkSheet(Guid projectId, WorkSheet workSheet)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\WorkSheets\\{2}.dat", AppDomain.CurrentDomain.BaseDirectory, projectId, workSheet.Id);
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }

            Utility.SerializeUtility.SerializeToBinaryFile(workSheet, filePath);
        }
        
        /// <summary>
        /// 保存工作项类别
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="workItemCategory"></param>
        public void SaveWorkItemCategory(Guid projectId, WorkItemCategory workItemCategory)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\WorkItemCategories\\{2}.dat",
                AppDomain.CurrentDomain.BaseDirectory,
                projectId,
                workItemCategory.Id);
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }

            Utility.SerializeUtility.SerializeToBinaryFile(workItemCategory, filePath);
        }

        public WorkItemCategory GetWorkItemCategory(Guid projectId, Guid workItemCategoryId)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\WorkItemCategories\\{2}.dat", 
                AppDomain.CurrentDomain.BaseDirectory,
                projectId,
                workItemCategoryId);
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }

            return Utility.SerializeUtility.DeserializeFromBinaryFile(filePath) as WorkItemCategory;
        }

        /// <summary>
        /// 保存工作项
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="workItem"></param>
        public void SaveWorkItem(Guid projectId, WorkItem workItem)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\WorkItems\\{2}.dat",
                AppDomain.CurrentDomain.BaseDirectory,
                projectId,
                workItem.Id);
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }

            Utility.SerializeUtility.SerializeToBinaryFile(workItem, filePath);
        }

        public WorkItem GetWorkItem(Guid projectId, Guid workItemId)
        {
            string filePath = string.Format("{0}\\CacheData\\{1}\\WorkItems\\{2}.dat",
                AppDomain.CurrentDomain.BaseDirectory,
                projectId,
                workItemId);
            if (!System.IO.File.Exists(filePath))
            {
                return null;
            }

            return Utility.SerializeUtility.DeserializeFromBinaryFile(filePath) as WorkItem;
        }
    }
}
